package com.easy.user.controller;

import com.easy.common.bean.ManagerBean;
import com.easy.common.bean.UserBean;
import com.easy.common.service.IManagerService;
import com.easy.common.service.IUserLoginService;
import com.easy.common.util.CaptchaUtil;
import com.easy.common.util.Result;
import com.easy.common.util.SYS;
import com.easy.common.util.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@CrossOrigin
public class UserLoginController {
    @Autowired
    IUserLoginService loginService;
    @Autowired // 新增：管理员服务
    IManagerService managerService;
    @GetMapping("/captcha")
    public Result captcha() {
        System.out.println("生成验证码");
        return Result.success(CaptchaUtil.getCaptcha());
    }

    @PostMapping("/dologin")
    public Result login(@RequestParam("loginType") String loginType, // 新增：登录类型参数
                        @RequestParam(value = "phone", required = false) String phone, // 用户登录用
                        @RequestParam(value = "adminAccount", required = false) String adminAccount, // 管理员登录用
                        @RequestParam("password") String password,
                        @RequestParam(value = "captcha", required = false) String captcha,
                        @RequestParam(value = "key", required = false) String key) {

        // 用户登录需要验证码验证
        if ("user".equals(loginType)) {
            if(!CaptchaUtil.checkCaptcha(key, captcha)){
                return Result.fail(500, "验证码错误");
            }
        }

        // 登录逻辑
        try{
            String token;
            if ("user".equals(loginType)) {
                // 用户登录逻辑
                UserBean user = new UserBean();
                user.setPhone(phone); // 假设UserBean有phone字段
                user.setPassword(password);
                token = loginService.login(user);
            } else {
                // 管理员登录逻辑
                token = managerService.adminLogin(adminAccount, password); // 需要新增adminLogin方法
            }
            return Result.success("登录成功", token);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "登录失败: " + e.getMessage());
        }
    }

    // 新增：用户注册接口
    @PostMapping("/register")
    public Result register(@RequestBody UserBean user) {
        try {
            int result = loginService.register(user); // 需要新增register方法
            if (result > 0) {
                return Result.success("注册成功");
            }
            return Result.fail(400, "注册失败");
        } catch (Exception e) {
            return Result.fail(500, "注册异常: " + e.getMessage());
        }
    }

    // 新增：忘记密码接口
    @PostMapping("/resetPassword")
    public Result resetPassword(@RequestParam("phone") String phone,
                                @RequestParam("realname") String realname,
                                @RequestParam("newPassword") String newPassword) {
        try {
            int result = loginService.resetPassword(phone, realname, newPassword); // 需要新增resetPassword方法
            if (result > 0) {
                return Result.success("密码重置成功");
            }
            return Result.fail(400, "密码重置失败");
        } catch (Exception e) {
            return Result.fail(500, "密码重置异常: " + e.getMessage());
        }
    }

    //获取登录信息
    @GetMapping("/logininfo")

    public Result logininfo(HttpServletRequest request) {
        String token = request.getHeader(SYS.TOKEN_AUTHORIATION);
        System.out.println(token);
        // 需要根据token中的用户类型返回不同的信息
        Object userType = TokenUtil.getOption(token,"ROLE"); // 需要修改TokenUtil
        System.out.println("UserType from token: " + userType);
        if ("user".equals(userType)) {
            UserBean user = loginService.logininfo(token);
            return Result.success(user);
        } else {
            ManagerBean manager = managerService.getManagerByToken(token); // 需要新增方法
            return Result.success(manager);
        }
    }
}






























